package state.util;

import java.util.Vector;

public class StringUtils {
  public static String[] subArray(String[] source, int offset, int length) {
    String[] des = new String[length - offset];
    System.arraycopy(source, offset, des, 0, length);
    return des;
  }
  
  public static String[][] subArray(String[][] source, int offset, int length) {
    String[][] des = new String[length - offset][];
    System.arraycopy(source, offset, des, 0, length);
    return des;
  }
  
  public static int[] subArray(int[] source, int offset, int length) {
    int[] des = new int[length - offset];
    System.arraycopy(source, offset, des, 0, length);
    return des;
  }
  
  public static String nvl(Object obj, String replace) {
    if (obj == null || "".equals(obj)) { 
      return replace;
    }
    return obj.toString();
  }
  
  public static String[] split(String original, String separator) {
    Vector nodes = new Vector();
    int index = original.indexOf(separator);
    while (index >= 0) {
      nodes.addElement(original.substring(0, index));
      original = original.substring(index + separator.length());
      index = original.indexOf(separator);
    }
    nodes.addElement(original);
    String[] result = new String[nodes.size()];
    if (nodes.size() > 0)
      for (int loop = 0; loop < nodes.size(); loop++)
        result[loop] = (String) nodes.elementAt(loop);
    return result;
  }

  public static void deleteElementAt(Object[] arr, int index) {
    System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
  }
  
  public static void deleteElementAt(int[] arr, int index) {
    System.arraycopy(arr, index + 1, arr, index, arr.length - 1 - index);
  }

  public static void addAll(Vector get, Vector put) {
    for (int i = 0; i < put.size(); i++) {
      get.addElement(put.elementAt(i));
    }
  }

  public static String join(Vector list, String seperator) {
    if (list == null) {
      return null;
    }
    if (list.size() == 0) {
      return "";
    }
    StringBuffer sb = new StringBuffer();
    if (seperator == null || seperator.equals("")) {
      seperator = ",";
    }
    for (int i = 0; i < list.size(); i++) {
      sb.append((String) list.elementAt(i));
      sb.append(seperator);
    }
    if (sb.toString().endsWith(seperator)) {
      sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
  }

  public static String replaceAll(String strSrc, String strFind, String strReplace) {
    if (strFind == null || strFind.length() == 0)
      return strSrc;
    int iLocation = 0;
    int iPrevLocation = 0;
    StringBuffer strResult = new StringBuffer();
    while ((iLocation = strSrc.indexOf(strFind, iLocation)) >= 0) {
      strResult.append(strSrc.substring(iPrevLocation, iLocation));
      strResult.append(strReplace);
      iLocation += strFind.length();
      iPrevLocation = iLocation;
    }
    strResult.append(strSrc.substring(iPrevLocation, strSrc.length()));
    return strResult.toString();
  }
}
